
public class MyList implements IList{
    static class Node{
        public int val;
        public Node next;
        public Node(int val){
            this.val = val;
        }
    }
    public Node head;
    public Node creatList(){
        Node node1 = new Node(23);
        Node node2 = new Node(34);
        Node node3 = new Node(45);
        Node node4 = new Node(56);
        Node node5 = new Node(67);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        this.head = node1;
        return head;
    }

    @Override
    public void addFirst(int data) {
        Node node = new Node(data);
        if(this.head == null){
            head = node;
        }else {
            node.next = head;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        Node node = new Node(data);
        if(this.head == null){
            this.head = node;
        }else{
            Node nodeFlag = head;
            Node pre = null;
            while(nodeFlag != null){
                pre = nodeFlag;
                nodeFlag = nodeFlag.next;
            }
            pre.next = node;
        }
    }

    @Override
    public void addIndex(int index, int date) {

    }

    @Override
    public boolean contain(int key) {
        Node nodeFlag = head;
        while(nodeFlag != null){
            if(nodeFlag.val == key){
                return true;
            }
        }
        return false;
    }

    @Override
    public void delete(int key) {
        Node nodeFlag = head.next;
        Node pre = head;
        while(nodeFlag != null){
            if(nodeFlag.val == key){
                pre.next = nodeFlag.next;
                nodeFlag = nodeFlag.next;
            }else{
                pre = nodeFlag;
                nodeFlag = nodeFlag.next;
            }
        }
        if(this.head.val == key){
            this.head = head.next;
            return;
        }
    }

    @Override
    public void display() {
        Node nodeFlag = head;
        while(nodeFlag != null){
            System.out.print(nodeFlag.val+" ");
            nodeFlag = nodeFlag.next;
        }
    }
    public Node middleNode(){
        Node nodeFlag = head ;
        int flag = 1;
        while(nodeFlag.next!= null){
            nodeFlag = nodeFlag.next;
            flag++;
        }
        nodeFlag = head;
        for (int i = 0; i < (flag/2); i++) {
            nodeFlag = nodeFlag.next;
        }
        System.out.println(nodeFlag.val);
        return nodeFlag;
    }
}
//静态方法设定链表的元素
/**
 * 设定链表的各个元素内容，return head
 * display
 * 计算个数
 * 头插，尾插
 *
 */
